package com.pingan.haofang.searchcloud.common.configuration;

import java.sql.SQLException;
import java.util.Optional;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import com.alibaba.druid.pool.DruidDataSource;
import com.pingan.haofang.gov.sm.account.common.SessionVisitor;
import com.pingan.haofang.gov.sm.account.common.UserThreadContext;

/**
 * 数据库配置
 *
 * @author zhangbi617
 * @date 02/05/2017
 */
@Configuration
@ConditionalOnProperty("spring.datasource.searchcloud.url")
@EnableJpaAuditing
public class DataSourceConfiguration {

    @Value("${spring.datasource.searchcloud.url}")
    private String url;

    @Value("${spring.datasource.searchcloud.username}")
    private String username;

    @Value("${spring.datasource.searchcloud.password}")
    private String password;

    @Value("${spring.datasource.searchcloud.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.searchcloud.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.searchcloud.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.searchcloud.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.searchcloud.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.searchcloud.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.searchcloud.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.searchcloud.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.searchcloud.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.searchcloud.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.searchcloud.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.searchcloud.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.datasource.searchcloud.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.searchcloud.filters}")
    private String filters;

    @Value("{spring.datasource.searchcloud.connectionProperties}")
    private String connectionProperties;

    @Bean(destroyMethod = "close", initMethod = "init")
    public DataSource dataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxWait(maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        dataSource.setFilters(filters);
        dataSource.setConnectionProperties(connectionProperties);

        return dataSource;
    }

    /**
     * JPA auditingProvider
     *
     * @return
     */
    @Bean
    public AuditorAware<Long> auditorProvider() {
    	return () -> Optional.ofNullable(UserThreadContext.getSessionVisitor())
    			.map(SessionVisitor::getAccountId)
    			.orElse(0L);
    }
}
